<?php
/*----------------------------------------------------------------------
#Multi Media Box - 
# ----------------------------------------------------------------------
# Copyright (C) 2007 You Joomla. All Rights Reserved.
# Designed by: Dmitry Ryshkin
# License: GNU, GPL  index.php 
# Website: http://www.youjoomla.com
------------------------------------------------------------------------*/
if( ! defined( '_JEXEC' ) ) {
	die( 'Direct Access to ' . basename( __FILE__ ) . ' is not allowed.' ) ;
}
jimport("joomla.application.application");

$mainframe->registerEvent( 'onBeforeDisplayContent', 'pop_box_media');

global $mainframe;
$mosConfig_absolute_path	= JPATH_ROOT;
$mosConfig_live_site 		= JURI :: base();

$linktag_boot="";
$linktag_boot.="<link rel='stylesheet' type='text/css' href='".$mosConfig_live_site."plugins/content/bot_mb/css/popbox.css' media='screen'/>\n";

$linktag_boot.="<script type='text/javascript' src='".$mosConfig_live_site."plugins/content/bot_mb/js/mootools.js'></script>\n";
$linktag_boot.="<script type='text/javascript' src='".$mosConfig_live_site."plugins/content/bot_mb/js/popbox.js'></script>\n";
$linktag_boot.="<script type='text/javascript' src='".$mosConfig_live_site."plugins/content/bot_mb/js/moviebox.js'></script>\n";

$linktag_boot.= "<!--[if lte IE 6]>
<link href=\"".$mosConfig_live_site."plugins/content/bot_mb/css/ie6fixes.css\" rel=\"stylesheet\" type=\"text/css\" />
<script type=\"text/javascript\" src=\"".$mosConfig_live_site."plugins/content/bot_mb/js/ie6minmax.js\"></script>
<![endif]-->
\n";
JApplication::addCustomHeadTag($linktag_boot);

function find_part($a,$s)
{
  for ($i=0;$i<count($a);$i++)
  {
    if (substr_count($a[$i],$s)>0)
    {
      return($i);
    }
  }
  return(-1);
}

function pop_box_media(&$row, &$params, $page = 0)
{
	global $mainframe;
	$mosConfig_absolute_path	= JPATH_ROOT;
	$mosConfig_live_site 		= JURI :: base();
	$database					=& JFactory::getDBO();

/*  	$query = "SELECT params"
	. "\n FROM #__mambots"
	. "\n WHERE element = 'bot_mb'"
	. "\n AND folder = 'content'"
	;
	$database->setQuery( $query );
	$database->loadObject($mambot);

	$params2 = new mosParameters( $mambot->params );*/
	$plugin 	=& JPluginHelper::getPlugin('content', 'bot_mb');
	$params_all = new JParameter( $plugin->params );
	$params2	= $params_all->_registry['_default']['data'];

    $image_folder=$params2->image_folder;
    $img_width=$params2->img_width;
    $img_height=$params2->img_height;

    $class_name="popbox";
    $regex = '/\{mbox:(.*?)}/i';

	preg_match_all($regex,$row->text,$matches);
	for($x=0; $x<count($matches[0]); $x++)
	{
		$parts = explode(" ",$matches[1][$x]);
        $href=$parts[0];

        if (find_part($parts,"title=")!=-1)
        {
          $t=explode("title=",$parts[find_part($parts,"title=")]);
          $title=$t[1];
        }else
        {
          $title="Empty title";
        }

        if (find_part($parts,"group=")!=-1)
        {
          $t=explode("group=",$parts[find_part($parts,"group=")]);
          $group='rel="'.$t[1].'"';
        }else
        {
          $group='';
        }

        if (find_part($parts,"txt=")!=-1)
        {
          $t=explode("txt=",$parts[find_part($parts,"txt=")]);
          $txt=$t[1];
        }else
        {
          $txt="Link";
        }

        $fp=explode('.',$href);

        if (isset($fp[1])&&($fp[1]!="htm")&&($fp[1]!="html"))
        {
          $replace="<a href=\"".$mosConfig_live_site."/".$image_folder."/".$href."\" title=\"".$title."\" class=\"".$class_name."\" ".$group.">";
          $replace.="<img src=\"".$mosConfig_live_site."/".$image_folder."/".$href."\" width=\"".$img_width."\" height=\"".$img_height."\" border=\"0\" alt=\"".$title."\"/>\n";
          $replace.="</a>\n";
        }else

        if (isset($fp[1])&&(($fp[1]=="htm")||($fp[1]=="html")))
        {
          $replace="<a href=\"".$mosConfig_live_site."/".$href."?keepThis=true&amp;POP_iframe=true&amp;height=".$parts[1]."&amp;width=".$parts[2]."\" title=\"".$title."\" class=\"".$class_name."\">".$txt."</a>\n";
        }
        else
        {
          $replace="<a href=\"".$mosConfig_live_site."index.php#POP_inline?height=".$parts[1]."&amp;width=".$parts[2]."&amp;inlineId=".$href."\" title=\"".$title."\" class=\"".$class_name."\">".$txt."</a>\n";
        }
        $row->text=str_replace($matches[0][$x],$replace,$row->text);
	}
}

?>
